﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SocialGame.Models;
using Ext.Net;
using SocialGame.Business;
using Social.Game.DAL;
using Omu.ValueInjecter;
using Ext.Net.MVC;

namespace SocialGame.Areas.Users.Controllers
{
    public class User_ManagermentController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return new Ext.Net.MVC.PartialViewResult();
        }

        [HttpPost]
        public ActionResult Create(RegisterModel model)
        {
            User user = new User();
            user.InjectFrom(model);
            user.CustomerName = model.FullName;
            user.BOD = model.BOD;
            user.State = model.Status ? UserStatus.ACTIVE : UserStatus.INACTIVE;
            user.RoleId = model.Role.ID;
            if (user.UserID == 0)
            {
                DbUtil.Database.Insert(user);
            }
            else
            {
                DbUtil.Database.Update(user);
            }
            return this.Direct();
        }

        public JsonResult CheckUsername(string username)
        {
            var count = DbUtil.Database.ExecuteScalar<int>("SELECT count(UserId) FROM Users Where UserName = @0", username);


            if (count > 0)
            {
                return Json(new { valid = false, message = "Tên đăng nhập đã tồn tại" }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { valid = true }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CheckEmail(string email)
        {
            var count = DbUtil.Database.ExecuteScalar<int>("SELECT count(UserId) FROM Users Where Email = @0", email);
            if (count > 0)
            {
                return Json(new { valid = false, message = "Địa chỉ email đã tồn tại" }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { valid = true }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetUsers(StoreRequestParameters parameters)
        {
            var db = DbUtil.Database;
            var page = db.Page<User>(parameters.Page, parameters.Limit, "SELECT * FROM Users");
            Paging<User> userPaging = new Paging<User>(page.Items, (int)page.TotalItems);
            return this.Store(userPaging);
        }


        public ActionResult EditCommand(string UserID, string command)
        {
            var db = DbUtil.Database;
            if ("Delete".Equals(command))
            {
                db.Delete("Users", "UserID", null, UserID);
                return this.Direct();
            }
            else
            {
                var user = db.SingleOrDefault<User>("SELECT * FROM Users WHERE UserId = @0", UserID);
                RegisterModel userModel = new RegisterModel();
                userModel.InjectFrom(user);
                userModel.BOD = user.BOD.HasValue ? user.BOD.Value : DateTime.Now;
                userModel.FullName = user.CustomerName;
                userModel.Password = user.Password;
                userModel.ConfirmPassword = user.Password;
                userModel.Role = new AccountRole { ID = user.RoleId };
                userModel.UserID = user.UserID;
                userModel.Status = user.State == UserStatus.ACTIVE ? true : false;
                return new Ext.Net.MVC.PartialViewResult
                {
                    ViewName = "Create",
                    Model = userModel,
                };
            }
        }


    }
}
